package zzs.generalframework.console.core.user.userbaseinner.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.user.userbaseinner.UserBaseInnerInfo;
import zzs.generalframework.common.core.user.userbaseinner.UserBaseInnerPO;
import zzs.generalframework.console.core.user.userbaseinner.dao.IUserBaseInnerDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.userbaseinner.dao.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.user.userbaseinner.dao.impl.userBaseInnerDaoImpl")
public class UserBaseInnerDaoImpl implements IUserBaseInnerDao
{
	private Logger logger = LoggerFactory.getLogger(UserBaseInnerDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(UserBaseInnerPO userBaseInnerPO)
	{
		logger.info("====插入用户基本数据开始====");
		logger.debug("====插入用户基本数据,入参:UserBaseInnerPO=[{}]====", userBaseInnerPO);
		entityManager.persist(userBaseInnerPO);
		logger.debug("====插入用户基本数据,出参:boolean=[{}]====", true);
		logger.info("====插入用户基本数据结束====");
		return true;
	}

	@Override
	public boolean update(UserBaseInnerPO userBaseInnerPO)
	{
		logger.info("====更新用户基本数据开始====");
		logger.debug("====更新用户基本数据,入参:UserBaseInnerPO=[{}]====", userBaseInnerPO);
		entityManager.merge(userBaseInnerPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新用户基本数据结束====");
		return true;
	}

	@Override
	public List<UserBaseInnerPO> select(UserBaseInnerInfo userBaseInnerInfo)
	{
		logger.info("====查询用户基本数据开始====");
		logger.debug("====查询用户基本数据,入参:UserBaseInnerInfo=[{}]====", userBaseInnerInfo);
		List<UserBaseInnerPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from UserBaseInnerPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userBaseInnerInfo.getUserBaseInnerID()))
		{
			sqlBuffer.append(" and a.userBaseInnerID=?");
			parameter.putParameterObject(userBaseInnerInfo.getUserBaseInnerID());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询用户基本数据,出参:List<UserBaseInnerPO>=[{}]====", returnList);
		logger.info("====查询用户基本数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(UserBaseInnerPO userBaseInnerPO)
	{
		logger.info("====删除用户基本数据开始====");
		logger.debug("====删除用户基本数据,入参:UserBaseInnerPO=[{}]====", userBaseInnerPO);
		entityManager.remove(userBaseInnerPO);
		logger.debug("====删除用户基本数据,出参:boolean=[{}]====", true);
		logger.info("====删除用户基本数据结束====");
		return true;
	}

	@Override
	public Long count(UserBaseInnerPO userBaseInnerPO)
	{
		logger.info("====统计用户基本数据开始====");
		logger.debug("====统计用户基本数据,入参:UserBaseInnerPO=[{}]====", userBaseInnerPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from UserBaseInnerPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userBaseInnerPO.getUserBaseInnerID()))
		{
			sqlBuffer.append(" and a.userBaseInnerID=?");
			parameter.putParameterObject(userBaseInnerPO.getUserBaseInnerID());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计用户基本数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计用户基本数据结束====");
		return returnLong;
	}

}
